import ajax from '../libs/ajax.js';
import md5 from '../libs/md5.min.js';

let txtUsername = document.getElementById('txtUsername');
let txtPwd = document.getElementById('txtPwd');
let txtNickname = document.getElementById('txtNickname');
let btnReg = document.getElementById('btnReg');

btnReg.addEventListener('click', () => {
  let user = {
    username: txtUsername.value.trim(),
    password: txtPwd.value.trim(),
    nickname: txtNickname.value.trim()
  };

  //密码校验
  if (user.password == '') {
    alert('密码必须填写');
    return;
  }
  user.password = md5.hash(user.password);

  //注册
  ajax.send('/user/auth/reg', user, (data) => {
    if (!data.success) {
      //成功跳转
      showToast(data.message);
      return;
    }
    location.href = './twtdr.html';
  });
});

//#region 公用的轻提示对话框

let liveToast = document.getElementById('liveToast');
let liveToastMessing = document.querySelector('#liveToast .toast-body');

const toast = new bootstrap.Toast(liveToast);

function showToast(message) {
  liveToastMessing.innerHTML = message;
  toast.show();
}
//#endregion
